//
//  SJPersonalHeaderView.m
//  WSL
//
//  Created by CJ on 2017/12/4.
//  Copyright © 2017年 sanjuetech. All rights reserved.
//

#import "SJPersonalHeaderView.h"
#import "SJUserInfoItem.h"


@interface SJPersonalHeaderView ()

@property (strong, nonatomic) UIView *contentView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (strong, nonatomic) UIImageView *avatarImageView;
@property (strong, nonatomic) UIImageView *coverImageView;
@property (strong, nonatomic) UIButton *settingButton;
@property (strong, nonatomic) UIImageView *genderImageView;
@property (nonatomic, strong) UILabel *introLabel;


@property (strong, nonatomic) UIButton *followButton;

@property (strong, nonatomic) UIView *userContainView;

@property (nonatomic, strong) UILabel *followLabel;
@property (nonatomic, strong) UILabel *followedLabel;
@property (nonatomic, strong) UILabel *publishedLabel;
@property (nonatomic, strong) UILabel *favLabel;

@end


@implementation SJPersonalHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setUserInfo:(SJUserInfoItem *)userInfo {
    
    _userInfo = userInfo;
    
    [self.avatarImageView setImageWithURL:[NSURL URLWithString:userInfo.avatar] placeholder:[UIImage imageNamed:@"common_icon_defaultavatar"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    
    if ([userInfo.cover isEqualToString:@""]) {
        self.coverImageView.image = [UIImage imageNamed:@"usershome_img_cover_placeholder"];
    } else {
        
        NSString *urlString = [NSString stringWithFormat:@"%@%@", SJImgURL, userInfo.cover];
        
        [self.coverImageView setImageWithURL:[NSURL URLWithString:urlString] placeholder:[UIImage imageNamed:@"usershome_img_cover_placeholder"] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    }
    
    self.nameLabel.text = userInfo.name;
    self.introLabel.text = userInfo.intro;
    
    NSString *idStr = [NSString stringWithFormat:@"ID %@", userInfo.user_code];
    self.idLabel.text = idStr;
    self.followLabel.text = userInfo.follow_num;
    self.followedLabel.text = userInfo.followed_num;
    self.publishedLabel.text = userInfo.published_num;
    self.favLabel.text = userInfo.fav_num;
    
    if ([userInfo.is_follow isEqualToString:@"1"]) {
        self.followButton.selected = NO;
    } else {
        self.followButton.selected = YES;
    }
    
    if ([userInfo.gender isEqualToString:@"1"]) {
        self.genderImageView.image = [UIImage imageNamed:@"Personal_icon_male"];
    } else {
        self.genderImageView.image = [UIImage imageNamed:@"Personal_icon_female"];
    }

}

- (void)setIsMe:(BOOL)isMe {
    
    _isMe = isMe;
    
    if (isMe) {
        _settingButton.hidden = NO;
        _followButton.hidden = YES;
        [self.userContainView setupAutoHeightWithBottomView:self.idLabel bottomMargin:0];
    } else {
        _settingButton.hidden = YES;
        _followButton.hidden = NO;
        [self.userContainView setupAutoHeightWithBottomView:self.followButton bottomMargin:0];
    }
}

- (void)setupUI {
    
    [self addSubview:self.contentView];
    self.contentView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    CGFloat h = SJItemWH * 6 + SJMargin * 5;
    CGFloat w = SJItemWH * 8 + SJMargin * 7;
    
    [self.contentView addSubview:self.coverImageView];
    self.coverImageView.sd_layout
    .topSpaceToView(self.contentView, SJMargin)
    .centerXEqualToView(self.contentView)
    .heightIs(h)
    .widthIs(w);
    
    [self.coverImageView addSubview:self.userContainView];
    self.userContainView.sd_layout
    .centerYEqualToView(self.coverImageView)
    .centerXEqualToView(self.coverImageView)
    .widthIs(w);
    
    [self.userContainView addSubview:self.avatarImageView];
    self.avatarImageView.sd_layout
    .topSpaceToView(self.userContainView, SJLayoutFrame(0))
    .centerXEqualToView(self.userContainView)
    .heightIs(SJLayoutFrame(80))
    .widthIs(SJLayoutFrame(80));
    
    [self.userContainView addSubview:self.nameLabel];
    self.nameLabel.sd_layout
    .topSpaceToView(self.avatarImageView, SJLayoutFrame(16))
    .centerXEqualToView(self.userContainView)
    .heightIs(SJLayoutFrame(17));
    [self.nameLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW * .5];
    
    
    [self.userContainView addSubview:self.genderImageView];
    self.genderImageView.sd_layout
    .leftSpaceToView(self.nameLabel, SJLayoutFrame(5))
    .centerYEqualToView(self.nameLabel)
    .heightIs(SJLayoutFrame(15))
    .widthIs(SJLayoutFrame(15));
    
    [self.userContainView addSubview:self.idLabel];
    self.idLabel.sd_layout
    .topSpaceToView(self.nameLabel, SJLayoutFrame(6))
    .centerXEqualToView(self.userContainView)
    .heightIs(SJLayoutFrame(14));
    [self.idLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW * .5];
    
    
    [self.userContainView addSubview:self.introLabel];
    self.introLabel.sd_layout
    .topSpaceToView(self.idLabel, SJLayoutFrame(10))
    .centerXEqualToView(self.userContainView)
    .heightIs(SJLayoutFrame(14));
    [self.introLabel setSingleLineAutoResizeWithMaxWidth:SJScreenW * .8];
    
    
    [self.userContainView addSubview:self.followButton];
    self.followButton.sd_layout
    .topSpaceToView(self.introLabel, SJLayoutFrame(25))
    .centerXEqualToView(self.userContainView)
    .heightIs(SJLayoutFrame(30))
    .widthIs(SJLayoutFrame(60));
//
//    [self.userContainView setupAutoHeightWithBottomView:self.followButton bottomMargin:0];
    
    [self.contentView addSubview:self.settingButton];
    self.settingButton.sd_layout
    .topSpaceToView(self.contentView, SJLayoutFrame(7))
    .rightSpaceToView(self.contentView, SJLayoutFrame(7))
    .heightIs(SJLayoutFrame(40))
    .widthIs(SJLayoutFrame(40));
    
    [self.contentView addSubview:self.numberBar];
    self.numberBar.sd_layout
    .leftSpaceToView(self.contentView, 0)
    .bottomSpaceToView(self.contentView, SJLayoutFrame(34))
    .heightIs(SJLayoutFrame(35.5))
    .widthIs(self.width_sd);
    
}

#pragma mark - getter

- (void)numberBarAction:(UIButton *)button {
    
    NSInteger index = button.tag;
    
    if ([self.delegate respondsToSelector:@selector(personalHeaderView:didSelectItemAtIndex:)]) {
        [(id<SJPersonalHeaderViewDelegate>)self.delegate personalHeaderView:self didSelectItemAtIndex:index];
    }
    
}

- (UIView *)numberBar {
    
    if (!_numberBar) {
        _numberBar = [UIView new];
        
        // 关注
        UIButton *button_0 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button_0 addTarget:self action:@selector(numberBarAction:) forControlEvents:UIControlEventTouchUpInside];
        button_0.tag = 0;
        [_numberBar addSubview:button_0];
        button_0.sd_layout
        .bottomSpaceToView(_numberBar, SJLayoutFrame(0))
        .leftSpaceToView(_numberBar, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(35.5))
        .widthIs(self.width_sd / 4);
        
        UILabel *label_0 = [UILabel new];
        label_0.textColor = SJColorHexString(@"#ffffff");
        label_0.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        label_0.textAlignment = NSTextAlignmentCenter;
        label_0.text = @"关注";
        [button_0 addSubview:label_0];
        label_0.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(18), 0, 0, 0));
        
        [button_0 addSubview:self.followLabel];
        self.followLabel.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, SJLayoutFrame(18), 0));
        
        UIView *line_0 = [UIView new];
        line_0.backgroundColor = SJColorHexString(@"#ffffff66");
        [button_0 addSubview:line_0];
        line_0.sd_layout.rightSpaceToView(button_0, 0).topSpaceToView(button_0, 0).bottomSpaceToView(button_0, 0).widthIs(1);
        
        // 关注者
        UIButton *button_1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button_1 addTarget:self action:@selector(numberBarAction:) forControlEvents:UIControlEventTouchUpInside];
        button_1.tag = 1;
        [_numberBar addSubview:button_1];
        button_1.sd_layout
        .bottomSpaceToView(_numberBar, SJLayoutFrame(0))
        .leftSpaceToView(button_0, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(35.5))
        .widthIs(self.width_sd / 4);
        
        UILabel *label_1 = [UILabel new];
        label_1.textColor = SJColorHexString(@"#ffffff");
        label_1.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        label_1.textAlignment = NSTextAlignmentCenter;
        label_1.text = @"关注者";
        [button_1 addSubview:label_1];
        label_1.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(18), 0, 0, 0));
        
        [button_1 addSubview:self.followedLabel];
        self.followedLabel.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, SJLayoutFrame(18), 0));
        
        UIView *line_1 = [UIView new];
        line_1.backgroundColor = SJColorHexString(@"#ffffff66");
        [button_1 addSubview:line_1];
        line_1.sd_layout.rightSpaceToView(button_1, 0).topSpaceToView(button_1, 0).bottomSpaceToView(button_1, 0).widthIs(1);
        
        
        // 发布
        UIButton *button_2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button_2 addTarget:self action:@selector(numberBarAction:) forControlEvents:UIControlEventTouchUpInside];
        button_2.tag = 2;
        [_numberBar addSubview:button_2];
        button_2.sd_layout
        .bottomSpaceToView(_numberBar, SJLayoutFrame(0))
        .leftSpaceToView(button_1, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(35.5))
        .widthIs(self.width_sd / 4);
        
        UILabel *label_2 = [UILabel new];
        label_2.textColor = SJColorHexString(@"#ffffff");
        label_2.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        label_2.textAlignment = NSTextAlignmentCenter;
        label_2.text = @"发布";
        [button_2 addSubview:label_2];
        label_2.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(18), 0, 0, 0));
        
        [button_2 addSubview:self.publishedLabel];
        self.publishedLabel.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, SJLayoutFrame(18), 0));
        
        UIView *line_2 = [UIView new];
        line_2.backgroundColor = SJColorHexString(@"#ffffff66");
        [button_2 addSubview:line_2];
        line_2.sd_layout.rightSpaceToView(button_2, 0).topSpaceToView(button_2, 0).bottomSpaceToView(button_2, 0).widthIs(1);
        
        
        // 收藏
        UIButton *button_3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [button_3 addTarget:self action:@selector(numberBarAction:) forControlEvents:UIControlEventTouchUpInside];
        button_3.tag = 3;
        [_numberBar addSubview:button_3];
        button_3.sd_layout
        .bottomSpaceToView(_numberBar, SJLayoutFrame(0))
        .leftSpaceToView(button_2, SJLayoutFrame(0))
        .heightIs(SJLayoutFrame(35.5))
        .widthIs(self.width_sd / 4);
        
        UILabel *label_3 = [UILabel new];
        label_3.textColor = SJColorHexString(@"#ffffff");
        label_3.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        label_3.textAlignment = NSTextAlignmentCenter;
        label_3.text = @"收藏";
        [button_3 addSubview:label_3];
        label_3.sd_layout.spaceToSuperView(UIEdgeInsetsMake(SJLayoutFrame(18), 0, 0, 0));
        
        [button_3 addSubview:self.favLabel];
        self.favLabel.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, SJLayoutFrame(18), 0));
    }
    return _numberBar;
}


- (UIView *)contentView {
    
    if (!_contentView) {
        
        _contentView = [UIView new];
    }
    return _contentView;
}

- (UIImageView *)coverImageView{
    
    if (!_coverImageView) {
        
        _coverImageView = [UIImageView new];
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView.sd_cornerRadius = @(SJCornerRadius);
        _coverImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverAction)];
        [_coverImageView addGestureRecognizer:singleTap];
        
    }
    return _coverImageView;
}

- (UIImageView *)genderImageView {
    
    if (!_genderImageView) {
        
        _genderImageView = [UIImageView new];
        _genderImageView.contentMode = UIViewContentModeScaleAspectFill;;
        
    }
    return _genderImageView;
}

- (void)coverAction {
    
    if (!self.isMe) {
        return;
    }
    
    if (self.coverTap) {
        self.coverTap(@"1");
    }
}

- (void)avatarAction {
    
    if (!self.isMe) {
        return;
    }
    
    if (self.avatarTap) {
        self.avatarTap(@"1");
    }
}

- (UIImageView *)avatarImageView{
    
    if (!_avatarImageView) {
        
        _avatarImageView = [UIImageView new];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
        _avatarImageView.sd_cornerRadiusFromHeightRatio = @(0.5);
        _avatarImageView.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
        _avatarImageView.layer.borderWidth = SJLayoutFrame(2);//边框宽度
        _avatarImageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarAction)];
        [_avatarImageView addGestureRecognizer:singleTap];
        
    }
    return _avatarImageView;
}

- (UIView *)userContainView {
    
    if (!_userContainView) {
        
        _userContainView = [UIView new];
    }
    return _userContainView;
}

- (UILabel *)nameLabel{
    
    if (!_nameLabel) {
        
        _nameLabel = [UILabel new];
        _nameLabel.textColor = SJColorHexString(@"#ffffff");
        _nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(17)];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UILabel *)introLabel {
    
    if (!_introLabel) {
        
        _introLabel = [UILabel new];
        _introLabel.textColor = SJColorHexString(@"#ffffff");
        _introLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        _introLabel.textAlignment = NSTextAlignmentCenter;
//        _introLabel.alpha = .6;
    }
    return _introLabel;
}

- (UILabel *)idLabel{
    
    if (!_idLabel) {
        
        _idLabel = [UILabel new];
        _idLabel.textColor = SJColorHexString(@"#ffffff");
        _idLabel.font = [UIFont systemFontOfSize:SJLayoutFrame(14)];
        _idLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _idLabel;
}

- (UIButton *)settingButton {
    
    if (!_settingButton) {
        
        _settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_settingButton setImage:[UIImage imageNamed:@"userhome_btn_setting"] forState:UIControlStateNormal];
        [_settingButton addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _settingButton;
}

- (void)settingAction {
    
    if (self.settingTap) {
        self.settingTap(@"1");
    }
}

- (UIButton *)followButton {
    
    if (!_followButton) {
        
        _followButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_followButton setImage:[UIImage imageNamed:@"Personal_icon_followe"] forState:UIControlStateNormal];
        [_followButton setImage:[UIImage imageNamed:@"Personal_icon_followed"] forState:UIControlStateSelected];
        [_followButton addTarget:self action:@selector(followAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _followButton;
}

- (void)followAction {
    
    if (self.followTap) {
        self.followTap(self.followButton);
    }
}

- (UILabel *)followLabel{
    
    if (!_followLabel) {
        
        _followLabel = [UILabel new];
        _followLabel.textColor = SJColorHexString(@"#ffffff");
        _followLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(16)];
        _followLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _followLabel;
}

- (UILabel *)followedLabel{
    
    if (!_followedLabel) {
        
        _followedLabel = [UILabel new];
        _followedLabel.textColor = SJColorHexString(@"#ffffff");
        _followedLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(16)];
        _followedLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _followedLabel;
}
- (UILabel *)publishedLabel{
    
    if (!_publishedLabel) {
        
        _publishedLabel = [UILabel new];
        _publishedLabel.textColor = SJColorHexString(@"#ffffff");
        _publishedLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(16)];
        _publishedLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _publishedLabel;
}
- (UILabel *)favLabel{
    
    if (!_favLabel) {
        
        _favLabel = [UILabel new];
        _favLabel.textColor = SJColorHexString(@"#ffffff");
        _favLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:SJLayoutFrame(16)];
        _favLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return _favLabel;
}

@end
